package com.lzyyj.gmis.base.model;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;

import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;

public class ExprotSTUtil {

	public void exportSTToWord(String titleName, String[][] list, File file) throws IOException {
//创建document对象
		XWPFDocument document = new XWPFDocument();
//添加标题
		XWPFParagraph titleParagraph = document.createParagraph();
//设置段落居中  
		titleParagraph.setAlignment(ParagraphAlignment.CENTER);
		XWPFRun titleParagraphRun = titleParagraph.createRun();
		titleParagraphRun.setText(titleName);
		titleParagraphRun.setColor("000000");
		titleParagraphRun.setFontSize(20);

//创建表格
		int row = list.length + 2;
		int column = list[0].length + 3;
		XWPFTable table = document.createTable(row, column);
		setTableWidth(table, "10000");
		// 处理表达合并和数据填充
		// 合并列
		mergeCellsVertically(table, 0, 0, 1);
		mergeCellsVertically(table, 2, 0, 1);
		mergeCellsVertically(table, column - 1, 0, 1);
		// 合并行
		for (int i = 0; i < row - 2; i++) {
			mergeCellsHorizontal(table, i, 0, 1);
			mergeCellsHorizontal(table, i, 2, 4);
		}
		mergeCellsHorizontal(table, 0, 5, column - 2);
		mergeCellsHorizontal(table, row - 2, 0, 4);
		mergeCellsHorizontal(table, row - 1, 0, 4);
		mergeCellsHorizontal(table, row - 1, 5, column - 1);
		// 填充数据
		XWPFTableRow rowIndex = table.getRow(0);
		XWPFTableCell cell = rowIndex.getCell(0);
		cell.setText(list[0][0]);
		XWPFTableCell cell1 = rowIndex.getCell(2);
		cell1.setText(list[0][1]);
		XWPFTableCell cell2 = rowIndex.getCell(5);
		cell2.setText("试题类型及题量");
		XWPFTableCell cell3 = rowIndex.getCell(column - 1);
		cell3.setText(list[0][list[0].length - 1]);
		XWPFTableRow rowIndex1 = table.getRow(1);
		for (int j = 5, i = 2; j < column - 1; j++, i++) {
			XWPFTableCell cell4 = rowIndex1.getCell(j);
			cell4.setText(list[0][i]);
		}
		for (int i = 2, ii = 1; i < row - 1; i++, ii++) {
			XWPFTableRow rowIndex2 = table.getRow(i);
			for (int j = 0, jj = 0; j < column - 1; j++) {
				XWPFTableCell cell4 = null;
				if (j == 0) {
					cell4 = rowIndex2.getCell(j);
					cell4.setText(list[ii][jj]);
					jj++;
				}
				if (j == 2) {
					cell4 = rowIndex2.getCell(j);
					cell4.setText(list[ii][jj]);
					jj++;
				} else if (j >= 5) {
					cell4 = rowIndex2.getCell(j);
					cell4.setText(list[ii][jj]);
					jj++;
				}
			}
		}

		XWPFTableRow rowIndex3 = table.getRow(row - 1);
		XWPFTableCell cell5 = rowIndex3.getCell(0);
		cell5.setText("其他需要说明的问题");
		OutputStream out = new FileOutputStream(file);
		document.write(out);
		out.close();
	}

	/***
	 * 跨行合并
	 * @param table
	 * @param col     合并列
	 * @param fromRow 起始行
	 * @param toRow   终止行
	 */

	private void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) {

		for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
			XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
			if (rowIndex == fromRow) {
				// The first merged cell is set with RESTART merge value
				cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
			} else {
				// Cells which join (merge) the first one, are set with CONTINUE
				cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
			}
		}
	}

	/***
	 * 跨列合并
	 * @param table
	 * @param row      所合并的行
	 * @param fromCell 起始列
	 * @param toCell   终止列
	 */
	private void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {
		for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {
			XWPFTableCell cell = table.getRow(row).getCell(cellIndex);
			if (cellIndex == fromCell) {
				// The first merged cell is set with RESTART merge value
				cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
			} else {
				// Cells which join (merge) the first one, are set with CONTINUE
				cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
			}
		}
	}
	
	/***
	 * 导出word 设置行宽
	 * @param table
	 * @param width
	 */
	private void setTableWidth(XWPFTable table, String width) {
		CTTbl ttbl = table.getCTTbl();
		CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();
		CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();
		CTJc cTJc = tblPr.addNewJc();
		cTJc.setVal(STJc.Enum.forString("center"));
		tblWidth.setW(new BigInteger(width));
		tblWidth.setType(STTblWidth.DXA);

	}

}
